/**
 * @author DELL
 * @createDate 2016/11/17 16:46
 * @copyright 广州市生物制品有限公司
 * @description 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.lzw.handler;

import com.lzw.event.AccountCreditedEvent;
import org.axonframework.domain.Message;
import org.axonframework.eventhandling.annotation.EventHandler;
import org.axonframework.eventhandling.annotation.Timestamp;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * 类功能描述: TODO
 *
 * @author 12360
 * @version 1.0
 * @createDate Nov 19, 2015 11:16:44 AM
 */
@Component
public class AccountCreditedEventHandler {

    @Autowired
    DataSource dataSource;

    @EventHandler
    public void handleAccountCreditedEvent(AccountCreditedEvent event, Message eventMessage, @Timestamp DateTime moment) {

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        String accountNo = event.getAccountNo();
        Double balance = event.getBalance();
        Double amountCredited = event.getAmountCredited();
        Double newBalance = balance + amountCredited;

        String updateQuery = "UPDATE account_view SET balance = ? WHERE account_no = ?";
        jdbcTemplate.update(updateQuery, new Object[]{newBalance, accountNo});
        System.out.println("Events Handled With EventMessage " + eventMessage.toString() + " at" + moment.toString());
    }

}
